import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author lzm
 * @date 2025/2/28 18:38
 * @description
 */
public class ThreadPoolStatus {

    private static AtomicInteger counter = new AtomicInteger(1);

    public static void main(String[] args) throws InterruptedException {
        CustomThreadPool threadPool = new CustomThreadPool(2, 2,
                1, TimeUnit.HOURS, new LinkedBlockingQueue<>(1),
                r -> new Thread(r, "线程-" + counter.getAndIncrement()),
                new ThreadPoolExecutor.AbortPolicy());
        Runnable handleOrderTask = () -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("处理用户订单的任务.....");
        };
        for (int i = 0; i < 3; i++) {
            threadPool.execute(handleOrderTask);
        }

        System.out.println("当前正在执行任务的线程数：" + threadPool.getActiveCount());
        System.out.println("队列中正在等待执行的任务数：" + threadPool.getQueue().size());

        System.out.println("线程池启动以来已经提交的任务总数(包括正在执行和已经完成的任务)：" + threadPool.getTaskCount());
        System.out.println("已执行完成的任务数：" + threadPool.getCompletedTaskCount());

        // 关闭线程池，不再接受新的任务
        threadPool.shutdown();
        boolean flag = threadPool.awaitTermination(1, TimeUnit.HOURS);
        if (flag) {
            System.out.println("线程池中的任务都执行完毕了");
        }
    }

}
